package com.ssyc.gsk_teacher_appraisal.activity;

import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.ssyc.common.base.BaseActivity;
import com.ssyc.common.base.BaseApplication;
import com.ssyc.common.bean.BusInfo;
import com.ssyc.common.http.HttpAdress;
import com.ssyc.common.http.HttpUtils;
import com.ssyc.common.manager.AccountUtils;
import com.ssyc.common.manager.BqaManager;
import com.ssyc.common.manager.OnloadingMoreListener;
import com.ssyc.common.manager.SrfManager;
import com.ssyc.common.utils.GsonUtil;
import com.ssyc.common.utils.UiUtils;
import com.ssyc.gsk_teacher_appraisal.R;
import com.ssyc.gsk_teacher_appraisal.adapter.StudentMainTestRvAdapter;
import com.ssyc.gsk_teacher_appraisal.bean.StudentTestInfo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;

/**
 * 学生端题库之测评界面
 * Created by Administrator on 2018/10/8 0008.
 */
@Route(path = "/teacherappraisal/TkStudentrAppraisalMainActivity")
public class TkStudentrAppraisalMainActivity extends BaseActivity implements SrfManager.OnPullRefreshListener, OnloadingMoreListener, View.OnClickListener {

    private ImageView ivBack;

    private LinearLayout llContent;

    private RelativeLayout rlLoadingView;

    //标题栏的布局
    private LinearLayout llTitle;

    //空视图
    private View emptyView;

    private RecyclerView rv;

    private SmartRefreshLayout srf;

    //数据列表集合
    private List<StudentTestInfo.DataBean> oldDatas;

    //适配器
    private StudentMainTestRvAdapter adapter;

    //通知页面刷新的标识
    public static final String UPDATEPAGEDATA = "updatepagedata";

    @Override
    protected boolean isNeedShowTitle() {
        return false;
    }

    @Override
    protected String setTitleText() {
        return null;
    }

    @Override
    protected int getContentLayout() {
        return R.layout.activity_tk_student_appraisal_main;
    }

    @Override
    protected void init() {

        showContent();

        //初始化视图
        initView();

        //初始化下拉刷新控件
        initSrf();

        //设置数据
        initRv();

        //请求页面列表数据
        httpPageData(BqaManager.NORML);
    }

    /**
     * 初始化视图
     */
    public void initView() {
        ivBack = findViewById(R.id.iv_back);
        ivBack.setOnClickListener(this);
        llContent = findViewById(R.id.ll_content);
        llContent.setVisibility(View.GONE);
        rlLoadingView = findViewById(R.id.rl_loading);
        //标题栏
        llTitle = findViewById(R.id.ll_title);
        rv = findView(R.id.rv);
        srf = findViewById(R.id.srf);
    }

    /**
     * 点击事件
     *
     * @param view
     */
    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.iv_back) {
            //返回
            finish();
        }
    }


    /**
     * 请求页面列表数据
     */
    private void httpPageData(int method) {

        Map<String, String> map = new HashMap<>();
        map.put("type", "31");
        map.put("student", AccountUtils.getAccount(BaseApplication.context));

        HttpUtils.post(HttpAdress.STUDENTMAIN, map, this, new HttpUtils.CallBack() {
            @Override
            public void onError(Call call, Exception e, int id) {
                if (rlLoadingView != null && rlLoadingView.getVisibility() == View.VISIBLE) {
                    rlLoadingView.setVisibility(View.GONE);
                }
                UiUtils.Toast(BaseApplication.ERROR, false);
            }

            @Override
            public void onResponse(String response, int id) {
                if (rlLoadingView.getVisibility() == View.VISIBLE) {
                    rlLoadingView.setVisibility(View.GONE);
                }

                if (llContent.getVisibility() == View.GONE) {
                    llContent.setVisibility(View.VISIBLE);
                }

                StudentTestInfo info = null;
                try {
                    info = GsonUtil.jsonToBean(response, StudentTestInfo.class);
                } catch (Exception e) {
                    Log.i("test", "数据解析异常");
                }

                if (null != info) {
                    if ("200".equals(info.getState())) {
                        //返回成功
                        List<StudentTestInfo.DataBean> newDatas = info.getData();

                        if (method == BqaManager.PULLREFRESH) {
                            BqaManager.updateRv(method, srf, rv, oldDatas, newDatas, adapter);
                        } else if (method == BqaManager.NORML) {
                            oldDatas.addAll(newDatas);
                            BqaManager.setRv(emptyView, TkStudentrAppraisalMainActivity.this, adapter, rv, TkStudentrAppraisalMainActivity.this);
                        }
                    } else {
                        //返回错误
                        UiUtils.Toast("服务器返回异常,状态码是:" + info.getState(), false);
                        Log.i("test", "服务器返回异常,状态码是:" + info.getState());
                    }
                } else {
                    Log.i("test", "服务器返回数据为空");
                }
            }
        });
    }


    /**
     * 设置列表
     */
    private void initRv() {
        oldDatas = new ArrayList<>();
        emptyView = View.inflate(this, R.layout.base_rv_common_empty_view_coffee_bg, null);
        TextView tvMessage = emptyView.findViewById(R.id.tv_content);
        tvMessage.setText("暂时没有新的测试");
        adapter = new StudentMainTestRvAdapter(this, R.layout.teacher_rv_main_test, oldDatas);
    }

    /**
     * 初始化下拉刷新
     */
    private void initSrf() {
        SrfManager.setSmartByPullListener(srf, this);
    }


    /**
     * 加载更多
     */
    @Override
    public void onLoadMore() {

    }


    /**
     * 下拉刷新
     *
     * @param refreshlayout
     */
    @Override
    public void onRefresh(RefreshLayout refreshlayout) {
        httpPageData(BqaManager.PULLREFRESH);
    }

    /**
     * EventBus处理事件
     *
     * @param busInfo
     */
    @Override
    public void busEvent(BusInfo busInfo) {
        super.busEvent(busInfo);
        if (busInfo != null && UPDATEPAGEDATA.equals(busInfo.msg)) {
            //学生提交做题后刷新页面数据
            httpPageData(BqaManager.PULLREFRESH);
        }
    }
}
